var AdminStudents = {
		
		from_id : 'form_els',
		
		form_popup_el: null,
		form_el: null,
		
		init: function() {
			form = dojo.byId(this.from_id);
			this.form_el = form;
			
			dojo.connect(form,"OnSubmit",function() {
				return AdminSubject.onFormSubmit(this);
			});
			
		},
	
		editSubject: function(json_data) {
			var data = data = eval("("+json_data+")");
			dojo.query('.form_els').forEach(function(el) {
				el.value = data[el.name];
			});
			dojo.byId('popup_header').text('Edit Product');
		//	AdminPage.showForm();
		},
		
		addSubject: function() {
			dojo.query('.form_els').forEach(function(el) {
				el.value ='';
			});
			dojo.byId('popup_header').text('Add Product');
		//	AdminPage.showForm();
		},
		onFormSubmit: function() {
			var data = {};
			dojo.query('.form_els').forEach(function(el) {
				data['f['+el.name+']'] = el.value;
			});
			
			MainPage.showMask('body');
			
			$.ajax({
				url: '/admin/subjects/store',
				type: 'POST',
				data: data,
				success: function(res, d, r) {
					MainPage.hideMask('body');
					
					var result = eval("(" +res+")");
					console.log(res);
					if (result.success == true) {
						document.location.href = '/admin/subjects/';
					} else {
						alert(result.errors.join("\n\r"));
					}
					
				},
				error: function(res, f, r) {
					MainPage.hideMask('body');
					alert('Some Errors Occured, please try later.');
				}
			});
			
			return false;
		}
};
dojo.addOnLoad(function(){
	AdminStudents.init();
	dojo.connect(dojo.byId('add_b'),'click',function(){
		AdminStudents.addSubject();
	});
	dojo.query('.stud_edit').forEach(function(el){
		dojo.connect(el, 'click',function(){
			AdminStudents.editSubject(dojo.query(el).attr('json'));
		});
	});
});